package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import top.drinyeit.nongmao.BR;
import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.common.ItemClick;
import top.drinyeit.nongmao.model.CartBean;

/**
 * Created by YJY on 2018/1/10.
 */

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {

    private ItemClick itemClick;
    private LayoutInflater inflater;
    private List<CartBean> data = null;

    public CartAdapter(Context context, List<CartBean> data, ItemClick itemClick) {
        this.data = data;
        inflater = LayoutInflater.from(context);
        this.itemClick = itemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = null;
        switch (viewType) {
            case 1: //cartHead
                binding = DataBindingUtil.inflate(inflater, R.layout.item_cart_header, parent, false);
                break;
            case 2: //cartContent
                binding = DataBindingUtil.inflate(inflater, R.layout.item_cart_content, parent, false);
                break;
            default:
                break;
        }
        ViewHolder viewHolder = null;
        if (binding != null) {
            viewHolder = new ViewHolder(binding.getRoot());
            viewHolder.setBinding(binding);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setVariable(BR.itemCart, data.get(position));
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return null == data ? 0 : data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }

    public List<CartBean> getData() {
        return data;
    }

    public void setData(List<CartBean> data) {
        this.data = data;
    }

    public void addData(List<CartBean> data) {
        this.data.addAll(data);
    }

    public void clear() {
        this.data.clear();
    }

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ViewDataBinding binding;

        ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        @Override
        public void onClick(View v) {
            if (itemClick != null) {
                itemClick.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
